bar.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from __future__ import unicode_literals
  16. import sys
  17. from . import Progress
  18. class Bar(Progress):
  19. width = 32
  20. suffix = '%(index)d/%(max)d'
  21. bar_prefix = ' |'
  22. bar_suffix = '| '
  23. empty_fill = ' '
  24. fill = '#'
  25. def update(self):
  26. filled_length = int(self.width * self.progress)
  27. empty_length = self.width - filled_length
  28. message = self.message % self
  29. bar = self.fill * filled_length
  30. empty = self.empty_fill * empty_length
  31. suffix = self.suffix % self
  32. line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix,
  33. suffix])
  34. self.writeln(line)
  35. class ChargingBar(Bar):
  36. suffix = '%(percent)d%%'
  37. bar_prefix = ' '
  38. bar_suffix = ' '
  39. empty_fill = '∙'
  40. fill = '█'
  41. class FillingSquaresBar(ChargingBar):
  42. empty_fill = '▢'
  43. fill = '▣'
  44. class FillingCirclesBar(ChargingBar):
  45. empty_fill = '◯'
  46. fill = '◉'
  47. class IncrementalBar(Bar):
  48. if sys.platform.startswith('win'):
  49. phases = (u' ', u'▌', u'█')
  50. else:
  51. phases = (' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█')
  52. def update(self):
  53. nphases = len(self.phases)
  54. filled_len = self.width * self.progress
  55. nfull = int(filled_len) # Number of full chars
  56. phase = int((filled_len - nfull) * nphases) # Phase of last char
  57. nempty = self.width - nfull # Number of empty chars
  58. message = self.message % self
  59. bar = self.phases[-1] * nfull
  60. current = self.phases[phase] if phase > 0 else ''
  61. empty = self.empty_fill * max(0, nempty - len(current))
  62. suffix = self.suffix % self
  63. line = ''.join([message, self.bar_prefix, bar, current, empty,
  64. self.bar_suffix, suffix])
  65. self.writeln(line)
  66. class PixelBar(IncrementalBar):
  67. phases = ('⡀', '⡄', '⡆', '⡇', '⣇', '⣧', '⣷', '⣿')
  68. class ShadyBar(IncrementalBar):
  69. phases = (' ', '░', '▒', '▓', '█')